package com.openbravo.beans;

import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JPanel;
import javax.swing.JViewport;
import javax.swing.Scrollable;

public class JFlowPanel extends JPanel
  implements Scrollable
{
  private int hgap = 5;
  private int vgap = 5;

  public JFlowPanel()
  {
    this(5, 5);
  }

  public JFlowPanel(int paramInt1, int paramInt2)
  {
    this.hgap = paramInt1;
    this.vgap = paramInt2;
  }

  public void setHorizontalGap(int paramInt)
  {
    this.hgap = paramInt;
  }

  public int getHorizontalGap()
  {
    return this.hgap;
  }

  public void setVerticalGap(int paramInt)
  {
    this.vgap = paramInt;
  }

  public int getVerticalGap(int paramInt)
  {
    return this.vgap;
  }

  private Dimension calculateFlowLayout(boolean paramBoolean)
  {
    Dimension localDimension1 = new Dimension(0, this.hgap);
    int i;
    if ((getParent() != null) && ((getParent() instanceof JViewport)))
    {
      JViewport localJViewport = (JViewport)getParent();
      i = localJViewport.getExtentSize().width;
    }
    else if (getParent() != null)
    {
      i = getParent().getWidth();
    }
    else
    {
      i = getWidth();
    }
    synchronized (getTreeLock())
    {
      int j = getComponentCount();
      int k = 0;
      int m = 0;
      int n = 0;
      for (int i1 = 0; i1 < j; i1++)
      {
        Component localComponent = getComponent(i1);
        if (!localComponent.isVisible())
          continue;
        Dimension localDimension2 = localComponent.getPreferredSize();
        if ((n == 0) || (n + this.hgap + localDimension2.width + this.hgap <= i))
        {
          n += this.hgap;
          if (paramBoolean)
            localComponent.setBounds(getPosition(n, i - localDimension2.width), localDimension1.height, localDimension2.width, localDimension2.height);
          n += localDimension2.width;
          if (localDimension2.height <= m)
            continue;
          m = localDimension2.height;
        }
        else
        {
          localDimension1.height += m + this.vgap;
          if (paramBoolean)
            localComponent.setBounds(getPosition(this.hgap, i - localDimension2.width), localDimension1.height, localDimension2.width, localDimension2.height);
          if (n > k)
            k = n;
          n = this.hgap + localDimension2.width;
          m = localDimension2.height;
        }
      }
      localDimension1.height += m + this.vgap;
      if (n > k)
        k = n;
      localDimension1.width = k;
    }
    return localDimension1;
  }

  private int getPosition(int paramInt1, int paramInt2)
  {
    if (getComponentOrientation() == ComponentOrientation.RIGHT_TO_LEFT)
      return paramInt2 - paramInt1;
    return paramInt1;
  }

  public Dimension getPreferredSize()
  {
    return calculateFlowLayout(false);
  }

  public Dimension getMinimumSize()
  {
    return calculateFlowLayout(false);
  }

  public Dimension getMaximumSize()
  {
    return calculateFlowLayout(false);
  }

  public Dimension getPreferredScrollableViewportSize()
  {
    return calculateFlowLayout(false);
  }

  public void doLayout()
  {
    calculateFlowLayout(true);
  }

  public boolean getScrollableTracksViewportHeight()
  {
    return getParent().getHeight() > getPreferredSize().height;
  }

  public boolean getScrollableTracksViewportWidth()
  {
    return getParent().getWidth() > getPreferredSize().width;
  }

  public int getScrollableUnitIncrement(Rectangle paramRectangle, int paramInt1, int paramInt2)
  {
    if (getComponentCount() == 0)
      return paramInt1 == 0 ? this.hgap : this.vgap;
    return paramInt1 == 0 ? getComponent(0).getWidth() + this.hgap : getComponent(0).getHeight() + this.vgap;
  }

  public int getScrollableBlockIncrement(Rectangle paramRectangle, int paramInt1, int paramInt2)
  {
    if (getComponentCount() == 0)
      return paramInt1 == 0 ? this.hgap : this.vgap;
    if (paramInt1 == 0)
    {
    	int i = getComponent(0).getWidth() + this.hgap;
      return paramRectangle.width / i * i;
    }
    int i = getComponent(0).getHeight() + this.vgap;
    return paramRectangle.width / i * i;
  }

  public void setEnabled(boolean paramBoolean)
  {
    synchronized (getTreeLock())
    {
      int i = getComponentCount();
      for (int j = 0; j < i; j++)
        getComponent(j).setEnabled(paramBoolean);
    }
    super.setEnabled(paramBoolean);
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.beans.JFlowPanel
 * JD-Core Version:    0.6.0
 */